package com.xi.jetcomposedemo.activity

import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.mapSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.xi.jetcomposedemo.ui.compose.AppSkeleton
import com.xi.jetcomposedemo.ui.theme.Gray400
import com.xi.jetcomposedemo.ui.theme.Yellow800
import kotlinx.parcelize.Parcelize

class TestStateRecoveryActivity : ComponentActivity() {

    interface ICity {
        val name: String
        var country: String
    }

    @Parcelize
    data class ParcelableCity(
        override val name: String,
        override var country: String,
    ): Parcelable, ICity

    data class City(
        override val name: String,
        override var country: String,
    ) : ICity

    /**
     * 恢复模式
     */
    enum class RecoveryMode(val desc: String) {
        Parcelable("Parcelable"),
        MapSaver("MapSaver"),
        ListSaver("ListSaver"),
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppSkeleton {
                CityScreen()
            }
        }
    }

    @Composable
    private fun CityScreen() {
        val (recoveryMode, setRecoveryMode) = rememberSaveable {
            mutableStateOf(RecoveryMode.Parcelable)
        }
        val (city, setCity) = when (recoveryMode) {
            RecoveryMode.Parcelable -> parcelableStateRecovery()
            RecoveryMode.MapSaver -> mapStateRecovery()
            RecoveryMode.ListSaver -> listStateRecovery()
        }
        Column {
            Row(
                horizontalArrangement = Arrangement.spacedBy(9.dp),
                modifier = Modifier
                    .padding(9.dp)
                    .horizontalScroll(rememberScrollState())
            ) {
                RecoveryMode.values().forEach { mode ->
                    val backgroundColor = if (mode == recoveryMode) Yellow800 else Gray400
                    Text(
                        text = "恢复模式：\n${mode.desc}",
                        modifier = Modifier
                            .clickable {
                                setRecoveryMode(mode)
                            }
                            .background(backgroundColor)
                            .padding(6.dp)
                    )
                }
            }

            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.padding(10.dp),
                horizontalArrangement = Arrangement.spacedBy(9.dp),
            ) {
                TextButton(
                    colors = ButtonDefaults.buttonColors(),
                    onClick = {
                        if (recoveryMode == RecoveryMode.Parcelable) {
                            setCity(ParcelableCity("Beijing", "China"))
                        } else {
                            setCity(City("Beijing", "China"))
                        }
                    }
                ) {
                    Text(text = "Click to change")
                }
                Text(text = "${city.country} - ${city.name}")
            }
        }

    }

    @Composable
    private fun parcelableStateRecovery(): MutableState<ICity> {
        return rememberSaveable {
            mutableStateOf(ParcelableCity("Madrid", "Spain"))
        }
    }

    @Composable
    private fun mapStateRecovery(): MutableState<ICity> {
        val nameKey = "Name"
        val countryKey = "Country"
        val citySaver = mapSaver<ICity>(
            save = { mapOf(nameKey to it.name, countryKey to it.country) },
            restore = { City(it[nameKey] as String, it[countryKey] as String) }
        )
        return rememberSaveable(stateSaver = citySaver) {
            mutableStateOf(City("Madrid", "Spain"))
        }
    }

    @Composable
    private fun listStateRecovery(): MutableState<ICity> {
        val citySaver = listSaver<ICity, Any>(
            save = { listOf(it.name, it.country) },
            restore = { City(it[0] as String, it[1] as String) }
        )
        return rememberSaveable(stateSaver = citySaver) {
            mutableStateOf(City("Madrid", "Spain"))
        }
    }

}